<template>
  <div id="app">
    <div id="pie" style="width: 400px; height: 400px"></div>
    <div id="bar" style="width: 400px; height: 400px"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      teacherCounts: {},
      tNum: [],
      month: [],
      option: {
        title: {
          text: "课程分类",
          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            // name: "Access From",
            type: "pie",
            radius: "50%",
            data: [],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      },
      option2: {
        title: {
          text: "2023年1月至今注册讲师图",
          left: "center",
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: [],
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: [],
          },
        ],
      },
    };
  },
  created() {},
  mounted() {
    this.pieChart();
    this.barChart();
  },
  methods: {
    pieChart() {
      let myChart = this.$echarts.init(document.getElementById("pie"));
      this.$http.post("/count/courseResult").then((res) => {
        this.option.series[0].data = res.data.data;
        myChart.setOption(this.option);
      });
    },
    barChart() {
      // 基于准备好的dom，初始化echarts实例  这个和上面的bar对应
      let myChart = this.$echarts.init(document.getElementById("bar"));
      this.$http.post("/count/teacherNum").then((res) => {
        this.teacherCounts = res.data.data;
        for (let i = 0; i < this.teacherCounts.length; i++) {
          this.tNum[i] = this.teacherCounts[i].tNum;
        }
        for (let i = 0; i < this.teacherCounts.length; i++) {
          this.month[i] = this.teacherCounts[i].month;
        }
        console.log(this.tNum);
        console.log(this.month);
        this.option2.series[0].data = this.tNum;
        this.option2.xAxis[0].data = this.month;
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(this.option2);
      });
    },
  },
};
</script>

<style scoped>
</style>
